package com.example.modulepicselector.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.PrimitiveIterator;

public class MyPagerAdapter extends PagerAdapter {

    private ArrayList<View> viewList;
    private ViewGroup mContainer;
    private int mPosition;
    private Object mObject;


    public MyPagerAdapter(ArrayList<View> viewList) {
        this.viewList = viewList;
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object object) {
        return view == object;
    }

    @NonNull
    @NotNull
    @Override
    public Object instantiateItem(@NonNull @NotNull ViewGroup container, int position) {
        View v=viewList.get(position);
        ViewGroup parent = (ViewGroup) v.getParent();
        if (parent != null) {
            parent.removeView(v);

        }

        ((ViewGroup)container).addView(viewList.get(position));
        return viewList.get(position);
    }


    @Override
    public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
        container.removeView(viewList.get(position));
    }


    @Override
    public int getItemPosition(@NonNull @NotNull Object object) {

        return POSITION_NONE;
    }

    public void setList(ArrayList<View> views) {
        this.viewList = views;
    }

    @Override
    public void destroyItem(@NonNull @NotNull View container, int position, @NonNull @NotNull Object object) {
        super.destroyItem(container, position, object);
        ((ViewPager) container).removeView((View) object);

    }


    @Override
    public void startUpdate(@NonNull @NotNull ViewGroup container) {
        super.startUpdate(container);

    }


}
